﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver.Builders;
using MongoDB.Driver;

namespace asMeishi.web.Models
{
    /// <summary>
    /// 微评实体类
    /// </summary>
    public class impression : MongoDocument
    {

        /// <summary>
        /// 评价的店
        /// </summary>
        public ObjectId targetId { get; set; }
        /// <summary>
        /// 评价时间
        /// </summary>
        [BsonDateTimeOptions(Kind= DateTimeKind.Local)]
        public DateTime datetime { get; set; }
        /// <summary>
        /// 评价人Id
        /// </summary>
        public ObjectId userId { get; set; }
        /// <summary>
        /// 评价内容
        /// </summary>
       public string content { get; set; }
        /// 印象评分 (0....5);4及4以上为推荐
       public int grade { get; set; }
        /// <summary>
        /// 目标类型
        /// </summary>
        public string targetType { get; set; }

    }
    public enum impressionGrade
    {
        未评价=0,
        最差评 = 1,
        差评=2,
        中评=3,
        好评=4,
        最好评=5
        

    }
    public enum FoodImpressionGrade
    {
        不知道 = 0,
        这不是人吃的 = 1,
        不怎么样 = 2,
        还可以 = 3,
        满好吃 = 4,
        非常美味 = 5


    }
    /// <summary>
    /// 微评控制类
    /// </summary>
    public class impressionC<T> where T:MongoDocument
    {
        public DataSession<MongoDataContext, impression> db = new DataSession<MongoDataContext, impression>();

        /// <summary>
        /// 新建一个impressionC实例 并对其 user , datetime 赋值
        /// </summary>
        /// <returns></returns>
        public impression createDefault(user user)
        {
            var impression = new impression();
            impression.datetime = DateTime.Now;
            impression.userId = user.Id;
            impression.targetType = typeof(T).Name;
            return impression;
        }

        /// <summary>
        /// 新建一个impressionC实例 并对其 user , datetime ,type 赋值
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public impression create(ObjectId id, user user)
        {
            var impression = createDefault(user);
            impression.targetId = id;
            impression.datetime = DateTime.Now;
            return impression;
        }


        /// <summary>
        /// 得到店的所有评价
        /// </summary>
        /// <param name="shopId"></param>
        /// <returns></returns>
        public IQueryable<impression> getByTargetId(ObjectId Id)
        {
            return db.Where(d => d.targetId == Id);
        }
        public int getNumByTargetId(ObjectId shopId)
        {
            return getByTargetId(shopId).Count();

        }
        public IQueryable<impression> getAll()
        {
            return db.Where(d => d.targetType == typeof(T).Name);

        }
        /// <summary>
        /// 根据商户id，和"印象"关键字的内容，检索出impression
        /// </summary>
        /// <param name="shopId"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public IQueryable<impression> getByTargetIdByContent(ObjectId Id, string content)
        {
            return db.Where(d =>d.targetType==typeof(T).Name && d.targetId == Id && d.content == content);
        }

        /// <summary>
        /// 根据商户id，和"印象"关键字的内容，删除出impression，反回bool
        /// </summary>
        /// <param name="shopId"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public bool delByShopIdByContent(ObjectId shopId, string content)
        {
            ///可能效率低，待解决*******************************
            ///
            var q = new Dictionary<string,object>();
            q.Add("shopId",shopId);
            q.Add("content",content);
            db.Delete(q);
                
                //(d => d.shopId == shopId && d.content == content);
            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="shopId"></param>
        /// <returns></returns>
        public bool delByShopId(string id)
        {
            db.Mquery.Remove(Query.EQ(BsonClassMap.LookupClassMap(typeof(impression)).GetMemberMap("targetId").ElementName, ObjectId.Parse(id)));
            
            return true;
        }
        public impression delById(string id)
        {
            var imp = db.GetById(id);
            db.Delete(id);
            return imp;
        }
        /// <summary>
        /// 得到用户的所有印象
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public IQueryable<impression> getByUser(ObjectId userId)
        {
            return db.Where(d => d.userId == userId);
        }

        /// <summary>
        /// 新建一个微评
        /// </summary>
        /// <param name="impression"></param>
        public impression add(impression impression)
        {
            return db.Add(impression);

            //内容存入关键字表
            //new keyWordC().add(impression.content);
        }

        /// <summary>
        /// 是否可以发布
        /// </summary>
        /// <param name="shopId"></param>
        /// <returns></returns>
        public bool isPublish(string shopId)
        {
            var dateOld = DateTime.MinValue;
            try
            {
                dateOld = getByUser(userC.loginUser.Id).OrderByDescending(d => d.datetime).First().datetime;
            }
            catch
            {
            }

            var dateNow = DateTime.Now;

            if (dateOld.AddSeconds(new config().publishInterval) < dateNow) return true;
            return false;
        }

    }
}